package com.ruoyi.web.api;

import com.ruoyi.category.service.ISysSubjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.web.controller.common.CaptchaController;
import com.ruoyi.web.controller.system.SysLoginController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class LoginApi extends BaseController {
    @Autowired
    private SysLoginController  loginController;
    @Autowired
    private CaptchaController captchaController;
    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysSubjectService subjectService;
    @Autowired
    private ISysPostService postService;

    /**
     * 登录
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        return loginController.login(loginBody);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user){
        ObjectUtil.checkNull(user.getPassword(),"密码为空");
        ObjectUtil.checkNull(user.getPostId(),"身份为空");
        ObjectUtil.checkNull(user.getUsername(),"账号为空");
        ObjectUtil.checkNull(user.getDeptName(),"单位为空");
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

//    @RequestMapping("/captchaImage")
//    public AjaxResult captchaImage(HttpServletResponse response) throws IOException {
//        return captchaController.getCode(response);
//    }

//    @RequestMapping("/deptTree")
//    public AjaxResult deptTree(SysDept dept){
//        List<SysDept> depts = deptService.selectAllDeptList();
//        return AjaxResult.success(deptService.buildDeptTree(depts));
//    }

    /**
     * 用户职务
     * @return
     */
    @GetMapping("/postList")
    public AjaxResult postList(){
        SysPost post = new SysPost();
        post.setType("1");
        post.setStatus("0");
        post.setDelFlag("0");
        List<SysPost> list = postService.selectPostList(post);
        return AjaxResult.success(list);
    }

    /**
     * 单位、部门
     * @return
     */
    @GetMapping("/deptTree")
    public AjaxResult deptTree(){
        List<SysDept> depts = deptService.selectAllDeptList();
        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }
}
